/**
 * CartComp
 * Created by 金辉 on 2017/1/11.
 */
Comps.CartComp = function (config) {
    this.compId = config.compId;
    this.config = config;
}
Comps.CartComp.prototype.init = function () {
    var ts = this;
    var needAsyncInit = this.config.needAsyncInit;
    var topic = this.config.topic;
    var topicList = topic.split(",");
    //订阅交互组件
    $.each(topicList, function () {
        CompMessage.subscribe(this, function () {
            ts.loadData();
        });
    })
    if (needAsyncInit) {
        this.loadData();
    }
    Utils.ajaxLoadComplate(this);
    //绑定事件
    this._bindEvent();
}
Comps.CartComp.prototype.loadData = function () {
    var ts = this;
    var compId = this.config.compId;
    //循环html清空
    $("#"+compId+"_product").html("");
    $.ajax({
        "url": ContextPath+"/cartComp/ajaxLoadData.json",
        data: {"compId": compId},
        "success": function (data) {
            ts._render(data.data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.CartComp.prototype._render = function (data) {
    var compId = this.config.compId;
    //上方总价
    var totalPriceEle = $("#" + compId + "_totalPrice");
    //悬浮框上的总价
    var priceEle = $("#" + compId + "_price");
    //悬浮框上的总数
    var numEle = $("#" + compId + "_num");
    var number;
    var price;
    if (!$.isEmptyObject(data)) {
        //商品总数
        number = data.totalNum;
        //商品总价
        price = data.totalPrice;
    } else {
        //商品总数
        number = 0;
        //商品总价
        price = 0;
    }
    totalPriceEle.html(price);
    priceEle.html(price);
    numEle.html(number);
    //
    this._renderUpFrame(data);
    if (this.config.isEasyMode == 0) {
        $("#" + this.config.compId + "_drop").remove();
    } else {
        this._renderDropFrame(data);
    }
}
Comps.CartComp.prototype._renderUpFrame = function () {
    var compId = this.config.compId;
    var imgUrlEle = $("#" + compId + "_img");
    imgUrlEle.prop("src", this.config.imgUrl);
}
Comps.CartComp.prototype._renderDropFrame = function (data) {
    var ts = this;
    var compId = this.config.compId;
    var productEle = $("#" + compId + "_product");
    productEle.html("");
    var limodel = $("#"+compId+"_addModule li");
    if ($.isEmptyObject(data)) {
        return;
    }
    var productList = data.productList;
    for (var i = 0; i < productList.length; ++i) {
        var product = productList[i];
        var newli = limodel.clone();
        var productId = newli.find(".j_productId");
        var attrId = newli.find(".j_attrId");
        var productName = newli.find(".j_productName");
        var productPrice = newli.find(".j_productPrice");
        var productNum = newli.find(".j_productNum");
        var productUrl = newli.find(".j_productUrl");
        var productImg = newli.find(".j_productImg");
        productId.html(product.productId);
        productName.html(product.productName);
        productName.prop("href", ContextPath+ts.config.productUrl +"&productUuid="+product.productId);
        attrId.html(product.attrId);
        productPrice.html(product.productPrice);
        productNum.html(product.productNum);
        productUrl.prop("href", ContextPath+ts.config.productUrl +"&productUuid="+product.productId);
        productImg.prop("src", product.productImg);
        productEle.append(newli);
    }
}
Comps.CartComp.prototype._deletePro = function (proEle) {
    var compId = this.config.compId;
    var productId = proEle.find(".j_productId").html().trim();
    var attrId = proEle.find(".j_attrId").html().trim();
    $.ajax({
        "url": ContextPath+"/cartComp/deletePro.json",
        "data": {"productId": productId, "attrId": attrId},
        success: function (data) {
            //如果删除成功
            var totalPriceEle = $("#" + compId + "_totalPrice");
            var priceEle = $("#" + compId + "_price");
            var numEle = $("#" + compId + "_num");
            if (!$.isEmptyObject(data)) {
                totalPriceEle.html(data.totalPrice);
                priceEle.html(data.totalPrice);
                numEle.html(data.totalNum);
            } else {
                totalPriceEle.html(0.00);
                priceEle.html(0.00);
                numEle.html(0)
            }
            proEle.remove();
        }
    })
}
Comps.CartComp.prototype._bindEvent = function () {
    var ts = this;
    var compId = this.config.compId;
    $("#" + compId).delegate(".j_deletePro", "click", function () {
        var proEle = $(this).parents(".j_productModel");
        ts._deletePro(proEle);
    });
    $("#"+compId).delegate(".j_cartUrl","click",function(){
        CompMessage.publish(ts.config.loginTopic,'',function(data){
            window.location.href = ContextPath+ts.config.cartShopUrl;
        });
    })
}
/**
 * CategoryComp
 * Created by 金辉 on 2017/1/11.
 */
Comps.CategoryComp = function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.CategoryComp.prototype.init = function () {
    var needAsyncInit = this.config.needAsyncInit;
    if (needAsyncInit) {
        this.ajaxLoadData();
    }else {
        Utils.ajaxLoadComplate(this);
    }
    this._bindEvent();
}
Comps.CategoryComp.prototype.ajaxLoadData = function () {
    var ts = this;
    var compId = this.config.compId
    $.ajax({
        "url": ContextPath+"/categoryComp/ajaxLoadData.json",
        data: {"compId": compId},
        "success": function (data) {
            ts.render(data.data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.CategoryComp.prototype.render = function (data) {
    var compId = this.config.compId;
    var showType = this.config.showType;
    var firstLevelNum = this.config.firstLevelNum;
    var dropDiv = $("#" + compId + "_dropDiv");
    var loopEle = $("#" + compId + "_loop");
    var liEle = loopEle.find("li");
    if (showType == 0) {
        dropDiv.prop("style", "display:block");
    }
    var length = data.length;
    if (length > firstLevelNum) {
        length = firstLevelNum;
    }
    for (var i = 0; i < length; ++i) {
        var catModel = data[i];
        var newEle = liEle.clone();
        //渲染一级分类，根据配置判断是否带二级分类
        newEle = this._renderFirstCat(newEle, catModel);
        //渲染右侧分类
        newEle = this._renderRitghtCat(newEle, catModel);
        loopEle.append(newEle);
    }
    liEle.remove();
}


/**
 * 渲染一级分类，根据配置判断是否带二级分类
 * @param m
 */
Comps.CategoryComp.prototype._renderFirstCat = function (newEle, m) {
    var showLevel = this.config.showLevel;
    var isShowImage = this.config.isShowImage;
    var secondLevelNum = this.config.secondLevelNum;
    var catImgEle = newEle.find(".j_catImg");
    var firstCatEle = newEle.find(".j_firstCat");
    var secLoopEle = newEle.find(".j_secCatLoop");
    var secAEle = secLoopEle.find(".j_secCat");
    if (isShowImage == 0) {
    	if(m.img == '' || m.img == undefined){
    		catImgEle.removeAttr("src");
    	}else{
    		catImgEle.prop("src", m.img);
    	}
    }
    firstCatEle.html(m.categoryName);
    firstCatEle.prop("href", m.url);
    if (showLevel == 1) {
        var secList = m.subCategory;
        var length = secList.length;
        if (length > secondLevelNum) {
            length = secondLevelNum;
        }
        for (var i = 0; i < length; ++i) {
            var secModel = secList[i];
            var newSecA = secAEle.clone();
            newSecA.prop("href", secModel.url);
            newSecA.html(secModel.categoryName);
            secLoopEle.append(newSecA);
        }
    }
    secAEle.remove();
    return newEle;
}

/**
 * 渲染右侧分类
 * @param m
 */
Comps.CategoryComp.prototype._renderRitghtCat = function (newEle, m) {
    var rightLoop = newEle.find(".j_rightLoop");
    var rightDl = rightLoop.find(".j_rightDl");

    var secList = m.subCategory;
    for (var i = 0; i < secList.length; ++i) {
        var secModel = secList[i];
        var newSecCat = rightDl.clone();
        var secCat = newSecCat.find(".j_rightSecCat");
        secCat.prop("href", secModel.url);
        secCat.html(secModel.categoryName);
        var thdCatLoop = newSecCat.find(".j_rightThdCatLoop");
        var thdCat = newSecCat.find(".j_rightThdCat");
        var thdCatList = secModel.subCategory;
        if (!$.isEmptyObject(thdCatList)) {
            for (var j = 0; j < thdCatList.length; ++j) {
                var thdModel = thdCatList[j];
                var newThdCat = thdCat.clone();
                newThdCat.prop("href", thdModel.url);
                newThdCat.html(thdModel.categoryName);
                thdCatLoop.append(newThdCat);
            }
        }
        thdCat.remove();
        rightLoop.append(newSecCat);
    }
    rightDl.remove();
    return newEle;
}
/**
 * FullSearchComp
 * Created by 金辉 on 2017/1/11.
 */
Comps.FullSearchComp = function(config){
    this.compId = config.compId;
    this.config = config;
}
Comps.FullSearchComp.prototype.init=function(){
    var needAsyncInit = this.config.needAsyncInit;
    var keyword = this._getUrlParam("keyword");
    if(needAsyncInit){
        this._loadData();
        Utils.ajaxLoadComplate(this);
    }
    if(!$.isEmptyObject(keyword)&&keyword!=""){
        $("#"+this.compId+"_keyword").val(keyword);
    }
    this._bindEvent();
}
Comps.FullSearchComp.prototype._loadData=function(){
    var isHaveHot = this.config.isHaveHot;
    var hotwords = this.config.hotWords;
    var compId = this.config.compId;
    var hotwordList="";
    var defaultSelect = this.config.defaultSelect;
    var typeProEle = $("#"+compId + "_typeProduct");
    var typeStoreEle = $("#"+compId + "_typeStore");
    var typeValueEle = $("#"+compId+"_typeValue");
    if(defaultSelect == 1){
        typeStoreEle.addClass("current");
        typeProEle.removeClass("current");
        typeValueEle.val("store");
    }else{
        typeProEle.addClass("current");
        typeStoreEle.removeClass("current");
        typeValueEle.val("product");
    }
    if(!$.isEmptyObject(hotwords)){
        hotwordList = hotwords.split(",");
    }
    var productSearchUrl=ContextPath+this.config.productSearchUrl;
    var compId = this.config.compId;

    var loopEle = $("#"+compId+"_hotwords");
    var hotwordEle = loopEle.find(".j_hotword");
    if(isHaveHot=="1"){
        for(var i=0;i<hotwordList.length;++i){
            var hotword = hotwordList[i];
            var newEle = hotwordEle.clone();
            newEle.prop("href",productSearchUrl+"&keyword="+hotword);
            newEle.html(hotword);
            loopEle.append(newEle);
        }
    }
    hotwordEle.remove();
}
Comps.FullSearchComp.prototype.search=function(searchType){
    var compId = this.config.compId;
    var searchUrl;
    if(searchType=="product"){
        searchUrl = ContextPath+this.config.productSearchUrl;
    }else{
        searchUrl = ContextPath+this.config.storeSearchUrl;
    }
    var keyword = $("#"+compId+"_keyword").val();
    if($.isEmptyObject(keyword)){
        keyword = "";
    }
    window.location.href=searchUrl+"&keyword="+keyword;
}
Comps.FullSearchComp.prototype._bindEvent=function(){
    var ts = this;
    var compId = this.config.compId;
    $("#"+compId+"_search").click(function(){
        var searchType = $("#"+compId+"_typeValue").val();
        ts.search(searchType);
    })
    $("#"+compId+"_searchType li").click(function(){
        $("#"+compId+"_typeValue").val($(this).data("searchtype"));
        $("#"+compId+"_searchType li").removeClass("current");
        $(this).addClass("current");
    })
}
Comps.FullSearchComp.prototype._getUrlParam=function(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]); return null; //返回参数值
}

/**
 * NavigationComp
 * Created by 金辉 on 2017/1/11.
 */
Comps.NavigationComp = function (config) {
    this.compId = config.compId;
    this.config = config;
}
Comps.NavigationComp.prototype.init = function () {
    var needAsyncInit = this.config.needAsyncInit;
    if (needAsyncInit) {
        this.ajaxLoad();
    }
}
Comps.NavigationComp.prototype.ajaxLoad = function () {
    var ts = this;
    var compId = this.config.compId;
    $.ajax({
        "url": ContextPath+"/navigationComp/ajaxLoadData.json",
        data: {"compId": compId},
        "success": function (result) {
            var data = result.data;
            ts._loadData(data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.NavigationComp.prototype._loadData = function (data) {
    var showNum = this.config.showNum;
    var compId = this.config.compId;
    var ulEle = $("#" + compId + "_channelUl");
    var modelEle = ulEle.find(".j_channel");
    if($.isEmptyObject(data)){
        modelEle.remove();
        return ;
    }
    var size = data.length;
    if(size>showNum){
        size = showNum;
    }
    for (var i = 0; i < size; ++i) {
        var channel = data[i];
        var newEle = modelEle.clone();
        var aEle = newEle.find("a");
        if ($.isEmptyObject(channel.channelUrl)) {
            aEle.prop("href", ContextPath+this.config.customUrl + channel.pageUuid);
        } else {
            aEle.prop("href", channel.channelUrl);
        }
        aEle.html(channel.channelName);
        ulEle.append(newEle);
    }
    modelEle.remove();
}
/**
 * NoticeComp
 * Created by 金辉 on 2017/1/11.
 */
Comps.NoticeComp = function(config) {
    this.compId = config.compId;
    this.config = config;
}
Comps.NoticeComp.prototype.init = function() {
    var needAsyncInit = this.config.needAsyncInit;
    var rollType = this.config.rollType;
    var _self = this;
    if (rollType == 1) {
        jQuery(".noticeWrap").slide({
            mainCell: ".bd ul",
            autoPlay: true,
            effect: "topMarquee",//向左滚动用leftMarquee
            vis: _self.config.showNum,
            interTime: 50,
            trigger: "click"
        });

    }else if(rollType == 2){
        jQuery(".noticeWrap").slide({
            mainCell: ".bd ul",
            autoPlay: true,
            effect: "leftMarquee",//向左滚动用leftMarquee
            vis: _self.config.showNum,
            interTime: 50,
            trigger: "click"
        });
    }
    if (needAsyncInit) {
        this.ajaxLoad();
    }else {
        Utils.ajaxLoadComplate(this);
    }
}
Comps.NoticeComp.prototype.ajaxLoad=function(){
    var ts = this;
    $.ajax({
        "url": ContextPath+"/noticeComp/ajaxLoadData.json",
        "data": { "contentCateUuid": ts.config.categoryId },
        "success": function(result) {
            var data = result.data;
            ts._loadData(data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.NoticeComp.prototype._loadData = function(data) {
    var showNum = this.config.showNum;
    var compId = this.config.compId;
    var rollType = this.config.rollType;
    var categoryUrl = ContextPath+this.config.categoryUrl;
    var categoryId = this.config.categoryId;
    var categoryNameEle = $("#" + compId + "_categoryName");
    var categoryUrlEle = $("#" + compId + "_categoryUrl");
    categoryNameEle.html(this.config.categoryName);
    categoryUrlEle.prop("href", categoryUrl+"&categoryUuid="+ categoryId);
    var loopEle = $("#" + compId + "_loop");
    var liEle = loopEle.find("li");
    for (var i = 0; i < data.length; ++i) {
        var content = data[i];
        var newEle = liEle.clone();
        //渲染被循环的li
        this._renderLi(newEle, content);
        loopEle.append(newEle);
    }
    liEle.remove();
    if (rollType == 1) {
        jQuery(".noticeWrap").slide({
            mainCell: ".bd ul",
            autoPlay: true,
            effect: "topMarquee",//向左滚动用leftMarquee
            vis: showNum,
            interTime: 50,
            trigger: "click"
        });
    }else if(rollType == 2){
        jQuery(".noticeWrap").slide({
            mainCell: ".bd ul",
            autoPlay: true,
            effect: "leftMarquee",//向左滚动用leftMarquee
            vis: showNum,
            interTime: 50,
            trigger: "click"
        });
    }
}
//渲染被循环的li
Comps.NoticeComp.prototype._renderLi = function(newEle, content) {
    var contentUrl = ContextPath+this.config.contentUrl;
    var contentUrlEle = newEle.find(".j_contentUrl");
    //var contentEle = newEle.find(".j_note");
    var titleEle = newEle.find(".j_title");
    contentUrlEle.prop("href", contentUrl + content.contentId);
    //contentEle.html(content.note);
    titleEle.html(content.title);
}
/**
 * ProductList
 * Created by 金辉 on 2017/1/11.
 */
/**
 * Created by 金辉 on 2017/1/11.
 */
Comps.ProductList = function(config){
    this.compId = config.compId;
    this.config = config;
}
//异步情况下渲染数据
Comps.ProductList.prototype.init=function(){
    var ts = this;
    var needAsyncInit = ts.config.needAsyncInit;
    var totalNum = $("#"+ts.config.compId+"_productTotalNum").html();
    if(needAsyncInit){
        ts.ajaxLoadData();
    }else{
        ts._initPages(totalNum,ts.config.pageShow);
    }
    this.bindEvent();
}
//ajax请求数据
Comps.ProductList.prototype.ajaxLoadData =function(queryData){
    var ts = this;
    var defaultData = {
        "compId":ts.config.compId,
        "keyword":ts._getUrlParam("keyword"),
        "categoryUuid":ts._getUrlParam("categoryUuid"),
        "sortType":ts._getUrlParam("sortType"),
        "sortBy":ts._getUrlParam("sortBy"),
        "attrValue":ts._getUrlParam("attrValue"),
        "nowPage":ts._getUrlParam("nowPage"),
        "pageShow":ts.config.pageShow

    }
    $.extend(defaultData,queryData);
    $.ajax({
        "url":ContextPath+"/productListComp/ajaxLoadData.json",
        data: defaultData,
        "success":function(data){
            ts._renderProdcutList(data);
            ts._initPages(data.totalNum,data.pageShow);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
//渲染商品列表
Comps.ProductList.prototype._renderProdcutList = function(data){
    var compId = this.config.compId;
    var productsEle = $("#"+compId+"_products");
    var noResultEle = $("#"+compId + "_noResult");
    var productLoopEle = $("#"+compId+"_productLoop");
    var productTotalNumEle = $("#"+compId+"_productTotalNum");
    var productEle = productsEle.find(".j_product").first();
    productsEle.find(".j_product").remove();
    var productList = data.productList;
    //循环渲染商品信息
    if(productList.length>0){
        productTotalNumEle.html(data.totalNum);
        noResultEle.remove();
        for(var i=0;i<productList.length;++i){
            var model = productList[i];
            var newEle = productEle.clone();
            //渲染商品Element
            this._renderProductInfo(newEle,model);
            productLoopEle.append(newEle);
        }
    }else{//若无商品，提示无商品
        productEle.remove();
    }
}
//渲染商品信息
Comps.ProductList.prototype._renderProductInfo=function(newEle,model){
    var productIdEle = newEle.find(".j_productId");
    //价格
    var picEle = newEle.find(".j_pic");
    //图片
    var picHoverEle = newEle.find(".j_picHover");
    //评分
    var scoreEle = newEle.find(".j_score");
    //商品名称
    var productNameEle = newEle.find(".j_productName");
    /*商品详情路径*/
    var productUrlEle = newEle.find(".j_productUrl");
    //商品推荐语
    var recommendEle = newEle.find(".j_recommend");
    //商品价格
    var priceEle = newEle.find(".j_price");
    //所属商户名称
    var storeName = newEle.find(".j_storeName");
    //所属商户logo
    var storeLogo = newEle.find(".j_storeLogo");
    productIdEle.html(model.productId);
    picEle.prop("src",model.pic);
    picEle.prop("alt",model.productName);
    picHoverEle.prop("src",model.pic);
    picHoverEle.prop("alt",model.productName);
    scoreEle.prop("style","width:"+model.score*20+"%");
    productNameEle.prop("href",ContextPath+this.config.productInfoUrl+"&productUuid="+model.productId);
    productNameEle.html(model.productName);
    productUrlEle.prop("href",ContextPath+this.config.productInfoUrl+"&productUuid="+model.productId);
    recommendEle.html(model.recommend);
    priceEle.html(model.price);
    storeName.prop("title",model.storeName);
    storeLogo.prop("src",model.storeLogo);
}
//初始化分页栏
Comps.ProductList.prototype._initPages = function(totalNum,pageShow){
    var ts = this;
    var compId = ts.config.compId;
    layui.use(['laypage', 'layer'], function() {
        var laypage = layui.laypage
            , layer = layui.layer;
        var pages = Math.ceil(totalNum/pageShow);
        if(pages<1){
            pages = 1;
        }
        var uri = window.location.href;
        var reg = new RegExp("(^|&)nowPage=([^&]*)");
        var r = uri.match(reg);
        if(!$.isEmptyObject(r)){
            var curr = r[2];
        }
        laypage({
            cont: compId+'_paging'
            , pages: pages //总页数
            , groups: 5 //连续显示分页数
            ,curr: curr
            ,jump: function(obj, first){
                if(!first) {
                    var words = $.isEmptyObject(r)?"":r[0];
                    ts.changeUrl(uri,words,"&nowPage="+obj.curr);
                    ts.ajaxLoadData();
                    //跳转锚点
                    $("html,body").animate({scrollTop:$("#"+ts.config.compId+"_productLoop").offset().top}, 0);
                }
            }
        });
    })
}
//无刷新修改URL
Comps.ProductList.prototype.changeUrl=function(uri,words,replaceWords){
    var title = document.title;
    var state=({
        url: uri, title: title
    });
    if(!$.isEmptyObject(words)&&""!=words){
        uri=uri.replace(words,replaceWords);
    }else{
        uri=uri+replaceWords;
    }
    window.history.pushState(state, title, uri);
}
//获取URL上参数
Comps.ProductList.prototype._getUrlParam=function(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]); return null; //返回参数值
}
//商品收藏
Comps.ProductList.prototype.collectProduct=function(element){
    var ts = this;
    var compId = this.config.compId;
    var productEle = $(element).parents(".j_product");
    var productIdEle = productEle.find(".j_productId");
    var productId = productIdEle.html().trim();
    var returnMessage = $("#"+compId+"_returnMessage");
    var collectNum = $("#"+compId+"_collectNum");
    var collectUrl = $("#"+compId+"_collectUrl");
    var url = ContextPath+"/productListComp/collectProduct.json";
    $.ajax({
        "url":url,
        "data":{"productUuid":productId},
        "success":function(data){
            var code = data.code;
            var totalNum = data.totalNum;
            if(code==1){
                collectNum.html(totalNum);
                collectUrl.prop("href", ContextPath+ts.config.favoriteProUrl);
                returnMessage.html("收藏成功！");
            }else if(code==2){
                collectNum.html(totalNum);
                collectUrl.prop("href", ContextPath+ts.config.favoriteProUrl);
                returnMessage.html("收藏失败！");
            }else if(code==3){
                collectNum.html(totalNum);
                collectUrl.prop("href", ContextPath+ts.config.favoriteProUrl);
                returnMessage.html("您已经收藏此商品了！");
            }else{
                returnMessage.html(data);
            }
            $("#"+compId+"_modal-collect-form").modal('show');
        },
        "error":function(data){
            alert(data.responseText);
        }
    })
}

Comps.ProductList.prototype.bindEvent = function(){
    var ts = this;
    var compId = ts.config.compId;
    $("#"+compId).delegate(".j_collectProduct","click",function(){
        var that = this;
        CompMessage.publish(ts.config.topic,'',function(data){
            ts.collectProduct(that);
        });
    })
    $("a[id^='"+compId+"_sort_']").click(function(){
        var nameValue = $(this).prop("name");
        var sortType = "1";
        var type = $(this).find("i").attr("class");
        if(type=="fa fa-long-arrow-up"){
            sortType="1";
        }else if(type=="fa fa-long-arrow-down"){
            sortType="2";
        }
        var queryData = {
            "sortBy":nameValue,
            "sortType":sortType,
            "nowPage":"1"
        }
        $(this).find('.fa').toggleClass('fa-long-arrow-down').toggleClass('fa-long-arrow-up');
        ts.ajaxLoadData(queryData);
    })
    $("#"+compId+"_productPicSmall").click(function(){
        $("#"+compId).removeClass("m-pdblock");
        $("#"+compId).addClass("m-pdlist");
        $("#"+compId+"_productPicBig").removeClass("active");
        $(this).removeClass("active");
    })
    $("#"+compId+"_productPicBig").click(function(){
        $("#"+compId).removeClass("m-pdlist");
        $("#"+compId).addClass("m-pdblock");
        $("#"+compId+"_productPicSmall").removeClass("active");
        $(this).removeClass("active");
    })
}
/**
 * Screen
 * Created by 金辉 on 2017/1/11.
 */
Comps.Screen = function (config) {
    this.compId = config.compId;
    this.config = config;
}
Comps.Screen.prototype.init = function () {
    var ts = this;
    var needAsyncInit = this.config.needAsyncInit;
    if(needAsyncInit){
        var attrValue = ts._getUrlParam("attrValue");
        var defaultData = {
            "compId":ts.config.compId,
            "keyword":ts._getUrlParam("keyword"),
            "categoryUuid":ts._getUrlParam("categoryUuid"),
            "sortType":ts._getUrlParam("sortType"),
            "sortBy":ts._getUrlParam("sortBy"),
            "attrValue":ts._getUrlParam("attrValue"),
            "nowPage":ts._getUrlParam("nowPage"),
            "attrValue":attrValue,
            "pageShow":this.config.pageShow
        }
        this.ajaxLoadData(defaultData);
    }else{
        Utils.ajaxLoadComplate(this);
    }
    this._bindEvent();
}
Comps.Screen.prototype.ajaxLoadData = function (defaultData) {
    var ts = this;
    var compId = ts.config.compId;
    var compEle = $("#" + compId);
    var attrValue = defaultData.attrValue;
    var categoryUuid = ts._getUrlParam("categoryUuid");
    if(!$.isEmptyObject(categoryUuid)){
        $.ajax({
            "url":ContextPath+"/screenComp/getCategoryName.json",
            "data":{"categoryUuid":categoryUuid},
            "dataType":"text",
            "async":false,
            "success": function (data) {
                if($.isEmptyObject(attrValue)){
                    attrValue="分类_"+data;
                }else{
                    attrValue="分类_"+data+"@"+attrValue;
                }
            }
        })
    }
    $.ajax({
        "url": ContextPath+"/screenComp/ajaxLoadData.json",
        "data": defaultData,
        "success": function (data) {
            if (!$.isEmptyObject(attrValue)) {
                ts._renderChooseAttr(compEle, attrValue, compId);
            }
            //渲染筛选模块
            ts._renderScreenMoudle(compEle, compId, data, attrValue);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.Screen.prototype._renderScreenMoudle = function (compEle, compId, screenModel, attrValue) {
    var brandList = screenModel.brands;
    var priceList = screenModel.prices;
    var attributeList = screenModel.attributeList;
    var categoryList = screenModel.cateList;
    if (!this._isHaveQuery("attributeBrand", attrValue) && brandList != null && brandList.length > 0) {
        //渲染品牌
        this._renderBrandLoop(compEle, compId, brandList);
    } else {//如果已经查询了该属性 就不显示该删选条件
        var brandModule = $("#" + compId + "_brandModule");
        brandModule.remove();
    }
    if (!this._isHaveQuery("attributePrice", attrValue) && priceList != null && priceList.length > 0) {
        //渲染价格
        this._renderPrice(compEle, compId, priceList);
    } else {//如果已经查询了该属性 就不显示该删选条件
        var priceModule = $("#" + compId + "_priceModule");
        priceModule.remove();
    }
    if (!this._isHaveQuery("categoryUuid", attrValue) && categoryList != null && categoryList.length > 1) {
        //渲染分类
        this._renderCategory(compEle, categoryList, compId);
    } else {//如果已经查询了该属性 就不显示该删选条件
        var categoryModule = $("#" + compId + "_categoryModule");
        categoryModule.remove();
    }
    //渲染其他属性
    this._loopAddOtherEle(compEle, attributeList, attrValue);

}
Comps.Screen.prototype._isHaveQuery = function (param, attrValue) {
    if (attrValue != null) {
        if(param=="categoryUuid"){
            var uri = window.location.href;
            var reg = new RegExp("(^|&)categoryUuid=([^&]*)");
            var r = uri.match(reg);
            if(!$.isEmptyObject(r)){
                return true;
            }
        }else {
            var attrs = attrValue.split("@");
            if (attrs != null) {
                for (var i = 0; i < attrs.length; ++i) {
                    var attr = attrs[i];
                    var keyAndValue = attr.split("_");
                    if (keyAndValue != null) {
                        if (keyAndValue[0] == param) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Comps.Screen.prototype._renderBrandLoop = function (compEle, compId, brandList) {
    var brandLoopEle = $("#" + compId + "_brandLoop");
    var brandEle = brandLoopEle.find(".j_brand").first();
    for (var i = 0; i < brandList.length; ++i) {//渲染默认全部品牌Element
        var brandModel = brandList[i];
        var pinyin = brandModel.brandEnName;
        var firstChar = pinyin.charAt(0);
        firstChar = firstChar.toLowerCase();
        var newEle = brandEle.clone();
        //渲染品牌
        this._renderBrandInfo(newEle, brandModel, firstChar);
        brandLoopEle.append(newEle);
    }
    //渲染查看更多时全部的品牌
    brandEle.remove();
}
Comps.Screen.prototype._renderBrandInfo = function (newEle, brandModel, firstChar) {
    var compId = this.config.compId;
    var brandUrlEle = newEle.find(".j_brandUrl").first();
    var brandImgEle = newEle.find(".j_brandImgUrl").first();
    var brandNameEle = newEle.find(".j_brandName").first();
//            brandUrlEle.attr("href",ContextPath+"/webpage/run?pageUuid=123&attrValue=attributeBrand_"+brandModel.getBrandName());
//            brandUrlEle.attr("");
    brandUrlEle.attr("onclick", "doSearch_" + compId + "(this,'attributeBrand','" + brandModel.brandName + "')");
    brandUrlEle.attr("title", brandModel.brandName);
    newEle.attr("data-initial", firstChar);
    brandImgEle.attr("src", brandModel.imageUrl1);
    brandNameEle.html(brandModel.brandName);
}
Comps.Screen.prototype._renderCategory = function (compEle, categoryList, compId) {
    var loopEle = compEle.find(".j_cateLoop").first();
    var moduleEle = loopEle.find(".j_cateModule").first();
    for (var i = 0; i < categoryList.length; ++i) {
        var categoryModel = categoryList[i];
        var newEle = moduleEle.clone();
        var valueEle = newEle.find(".j_value").first();
        var categoryUuidEle = newEle.find(".j_categoryUuid").first();
        categoryUuidEle.val(categoryModel.uuid);
        valueEle.html(categoryModel.categoryName);
        valueEle.attr("onclick", "doSearch_" + compId + "(this,'categoryUuid','" + categoryModel.uuid + "')");
        loopEle.append(newEle);
    }
    moduleEle.remove();
}
Comps.Screen.prototype._renderPrice = function (compEle, compId, priceList) {
    var loopEle = $("#" + compId + "_priceLoop");
    var moduleEle = loopEle.find(".j_priceModule").first();
    for (var i = 0; i < priceList.length; ++i) {
        var str = priceList[i];
        var newEle = moduleEle.clone();
        var valueEle = newEle.find(".j_value").first();
        if (i == 0) {
            str = "0-" + str;
        } else if (i == priceList.length - 1) {
            str += "以上";
        }
        valueEle.attr("onclick", "doSearch_" + compId + "(this,'attributePrice','" + str + "')");
        valueEle.html(str);
        loopEle.append(newEle);
    }
    //删除模板
    moduleEle.remove();
}
Comps.Screen.prototype._loopAddOtherEle = function (compEle, attributeList, attrValue) {
    var otherEle = compEle.find(".j_other").first();
    for (var i = 0; i < attributeList.length; ++i) {
        var attributeModel = attributeList[i];
        if (this._isHaveQuery(attributeModel.attributeName, attrValue)) {
            continue;
        }
        var newEle = otherEle.clone();
        var attrNameEle = newEle.find(".j_attrName").first();
        attrNameEle.html(attributeModel.attributeName + "：");
        this._renderOtherAttr(newEle, attributeModel.attributeName, attributeModel.selectValue, attributeModel.unit);
        compEle.append(newEle);
    }
    otherEle.remove();
}
Comps.Screen.prototype._renderOtherAttr = function (otherEle, attrName, attributeList, unit) {
    var compId = this.config.compId;
    var loopEle = otherEle.find(".j_attrLoop").first();
    var moduleEle = otherEle.find(".j_attrModule").first();
    for (var i = 0; i < attributeList.length; ++i) {
        var str = attributeList[i];
        var newEle = moduleEle.clone();
        var valueEle = newEle.find(".j_value").first();
        valueEle.attr("onclick", "doSearch_" + compId + "(this,'" + attrName + "','" + str + "')");
        valueEle.html(str + unit);
        loopEle.append(newEle);
    }
    //删除模板
    moduleEle.remove();
}
Comps.Screen.prototype._renderChooseAttr = function (compEle, attrValue, compId) {
	
    var chooseAttrEle = $("#" + compId + "_chooseAttributesDIV");
    var loopEle = $("#" + compId + "_chooseAttributes");
    var attrEle = loopEle.find(".j_attribute").first();
    var attrArray = attrValue.split("@");
    for (var i = 0; i < attrArray.length; ++i) {
        var attr = attrArray[i];
        if(attr!=""){
        	  var newEle = attrEle.clone();
              var nameEle = newEle.find(".j_attrName").first();
              var valueEle = newEle.find(".j_attrValue").first();
              var nameAndValue = attr.split("_");
              newEle.attr("data-name", nameAndValue[0]);
              if ("attributeBrand" == nameAndValue[0]) {
                  nameEle.html("品牌");
              } else if ("attributePrice" == nameAndValue[0]) {
                  nameEle.html("价格");
              } else {
                  nameEle.html(nameAndValue[0]);
              }
              valueEle.html(nameAndValue[1]);
              loopEle.append(newEle);
        }
    }
    attrEle.remove();
    chooseAttrEle.attr("style", "display:block");
}
Comps.Screen.prototype._getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]);
    return null; //返回参数值
}
Comps.Screen.prototype._bindEvent = function () {
    var compId = this.config.compId;
    //更多事件
    $(".j_extMore").click(function () {
        $(this).parents(".sl-wrap").toggleClass("extend");
        $(".j_brand").prop("style", "display:block");
    })
    //拼音首字母覆盖事件
    $(".j_brandLetter li").hover(function () {
        $(".j_brand").prop("style", "display:none");
        var firstWord = $(this).data("initial");
        $(".j_brand").each(function () {
            var initial = $(this).data("initial");
            if (initial == firstWord || firstWord == 0) {
                $(this).prop("style", "display:block");
            }
        })
    })
    //多选事件
    $("#" + compId).delegate(".j_extMultiple", "click", function () {
        $(this).parents(".sl-wrap").addClass("multiple");
        $(".j_brand").prop("style", "display:block");
        $(".sl-ext").prop("style", "display:none");
    })
    //多选内的取消按钮事件
    $("#" + compId).delegate(".j_btnsCancel", "click", function () {
        $(this).parents(".sl-wrap").removeClass("multiple");
        $(".sl-ext").prop("style", "display:block");
        var selectedDiv = $(this).parents(".sl-wrap").find(".j_selectedDiv");
        selectedDiv.data("attrName", "");
        selectedDiv.data("attrValue", "");
        selectedDiv.find("ul").html("");
        selectedDiv.hide();
        $(".j_btnsConfirm").addClass("disabled");
    })
    // 条件删除效果
    $("#" + compId).delegate('.fa', 'click', function () {
        var attrName = $(this).parents('.j_attribute').data("name");
        $(this).parents('.j_attribute').remove();
        var uri = window.location.href;
        if("分类"==attrName){
            var reg = new RegExp("(^|&)categoryUuid=([^&]*)");
            var r = uri.match(reg);
            uri = uri.replace(r[0], "");
        }else{
            var reg = new RegExp("(^|&)attrValue=([^&]*)");
            var r = uri.match(reg);
            if (!$.isEmptyObject($("#" + compId + "_chooseAttributes").html().trim())) {
                var attrRep = r[2].match(new RegExp("(^|%40)" + encodeURI(attrName) + "_(((?!%40).)*)"));
                uri = uri.replace(attrRep[0], "");
            } else {
                uri = uri.replace(r[0], "");
            }
        }
        window.location.href = uri;
    })
    //删除所有条件
    $("#" + compId).delegate("#" + compId + "_clearAll", "click", function () {
        var uri = window.location.href;
        var reg = new RegExp("(^|&)attrValue=([^&]*)");
        var r = uri.match(reg);
        if(!$.isEmptyObject(r)){
            uri = uri.replace(r[0], "");
        }
        reg = new RegExp("(^|&)categoryUuid=([^&]*)");
        r = uri.match(reg);
        if(!$.isEmptyObject(r)){
            uri = uri.replace(r[0], "");
        }
        window.location.href = uri;
    })
    /* $("#"+compId+"_clearAll").click(function(){
     var uri = window.location.href;
     var reg = new RegExp("(^|&)attrValue=([^&]*)");
     var r = uri.match(reg);
     uri=uri.replace(r[0],"");
     window.location.href = uri;
     })*/
    $("#" + compId).delegate(".j_selectedAttr", "click", function () {
        var attrValue = $(this).parents(".j_selectedDiv").data("attrValue");
        var thisAttr = $(this).find("a").prop("title")
        var reg = new RegExp("(^|、)" + thisAttr + "([^、]*)");
        var r = attrValue.match(reg);
        $(this).parents(".j_selectedDiv").data("attrValue", attrValue.replace(r[0], ""));
        $(this).remove();
    })
}
/**
 * StoreCategoryComp
 * Created by 金辉 on 2017/1/11.
 */
Comps.StoreCategoryComp = function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.StoreCategoryComp.prototype.init = function () {
    var needAsyncInit = this.config.needAsyncInit;
    if (needAsyncInit) {
        this.ajaxLoad();
    }else{
        Utils.ajaxLoadComplate(this);
    }
}
Comps.StoreCategoryComp.prototype.ajaxLoad = function () {
    var ts = this;
    var compId = this.config.compId
    $.ajax({
        "url": ContextPath+"/storeCategoryComp/ajaxLoadData.json",
        data: {"compId": compId, "storeUuid": ts._getUrlParam("storeUuid")},
        "success": function (data) {
            ts._loadData(data.data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.StoreCategoryComp.prototype._loadData = function (data) {
    var compId = this.config.compId;
    var showType = this.config.showType;
    var firstLevelNum = this.config.firstLevelNum;
    var dropDiv = $("#" + compId + "_dropDiv");
    var loopEle = $("#" + compId + "_loop");
    var liEle = loopEle.find("li");
    if (showType == 0) {
        dropDiv.prop("style", "display:block");
    }
    var length = data.length;
    if (length > firstLevelNum) {
        length = firstLevelNum;
    }
    for (var i = 0; i < length; ++i) {
        var catModel = data[i];
        var newEle = liEle.clone();
        //渲染一级分类，根据配置判断是否带二级分类
        newEle = this._renderFirstCat(newEle, catModel);
        //渲染右侧分类
        newEle = this._renderRitghtCat(newEle, catModel);
        loopEle.append(newEle);
    }
    liEle.remove();
}

/**
 * 渲染一级分类，根据配置判断是否带二级分类
 * @param m
 */
Comps.StoreCategoryComp.prototype._renderFirstCat = function (newEle, m) {
    var showLevel = this.config.showLevel;
    var isShowImage = this.config.isShowImage;
    var secondLevelNum = this.config.secondLevelNum;
    var catImgEle = newEle.find(".j_catImg");
    var firstCatEle = newEle.find(".j_firstCat");
    var secLoopEle = newEle.find(".j_secCatLoop");
    var secAEle = secLoopEle.find(".j_secCat");
    if (isShowImage == 0) {
        catImgEle.prop("src", m.img);
    }
    firstCatEle.html(m.categoryName);
    firstCatEle.prop("href", m.url);
    if (showLevel == 1) {
        var secList = m.subCategory;
        var length = secList.length;
        if (length > secondLevelNum) {
            length = secondLevelNum;
        }
        for (var i = 0; i < length; ++i) {
            var secModel = secList[i];
            var newSecA = secAEle.clone();
            newSecA.prop("href", secModel.url);
            newSecA.html(secModel.categoryName);
            secLoopEle.append(newSecA);
        }
    }
    secAEle.remove();
    return newEle;
}

/**
 * 渲染右侧分类
 * @param m
 */
Comps.StoreCategoryComp.prototype._renderRitghtCat = function (newEle, m) {
    var rightLoop = newEle.find(".j_rightLoop");
    var rightDl = rightLoop.find(".j_rightDl");

    var secList = m.subCategory;
    for (var i = 0; i < secList.length; ++i) {
        var secModel = secList[i];
        var newSecCat = rightDl.clone();
        var secCat = newSecCat.find(".j_rightSecCat");
        secCat.prop("href", secModel.url);
        secCat.html(secModel.categoryName);
        var thdCatLoop = newSecCat.find(".j_rightThdCatLoop");
        var thdCat = newSecCat.find(".j_rightThdCat");
        var thdCatList = secModel.subCategory;
        if (!$.isEmptyObject(thdCatList)) {
            for (var j = 0; j < thdCatList.length; ++j) {
                var thdModel = thdCatList[j];
                var newThdCat = thdCat.clone();
                newThdCat.prop("href", thdModel.url);
                newThdCat.html(thdModel.categoryName);
                thdCatLoop.append(newThdCat);
            }
        }
        thdCat.remove();
        rightLoop.append(newSecCat);
    }
    rightDl.remove();
    return newEle;
}
//获取URL上参数
Comps.StoreCategoryComp.prototype._getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]);
    return null; //返回参数值
}
/**
 * StoreList
 * Created by 金辉 on 2017/1/11.
 */
Comps.StoreList = function (config) {
    this.compId = config.compId;
    this.config = config;
}
//异步情况下渲染数据
Comps.StoreList.prototype.init = function () {
    var ts = this;
    var needAsyncInit = ts.config.needAsyncInit;
    var totalNum = $("#" + ts.config.compId + "_storeTotalNum").html();
    if (needAsyncInit) {
        ts.ajaxLoadData();
    } else {
        ts._initPages(totalNum, ts.config.pageShow);
        Utils.ajaxLoadComplate(this);
    }
    this._bindEvent();
}
//ajax请求数据
Comps.StoreList.prototype.ajaxLoadData = function (queryData) {
    var ts = this;
    var defaultData = {
        "compId": ts.config.compId,
        "keyword": ts._getUrlParam("keyword"),
        "nowPage": ts._getUrlParam("nowPage"),
        "pageShow": ts.config.pageShow
    }
    $.extend(defaultData, queryData);
    $.ajax({
        "url": ContextPath+"/storeListComp/ajaxLoadData.json",
        data: defaultData,
        "success": function (data) {
            ts._renderStroreList(data);
            //跳转锚点
            $("html,body").animate({scrollTop: $("#" + ts.config.compId + "_storeLoop").offset().top}, 0);
            ts._initPages(data.totalNum, data.pageShow);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
//渲染商品列表
Comps.StoreList.prototype._renderStroreList = function (data) {
    var compId = this.config.compId;
    var storesEle = $("#" + compId + "_stores");
    var noResultEle = $("#" + compId + "_noResult");
    var storeLoopEle = $("#" + compId + "_storeLoop");
    var storeTotalNumEle = $("#" + compId + "_storeTotalNum");
    var storeEle = storesEle.find(".j_store").first();
    storesEle.find(".j_store").remove();
    var storeList = data.storeList;
    //循环渲染商品信息
    if (storeList.length > 0) {
        storeTotalNumEle.html(data.totalNum);
        noResultEle.remove();
        for (var i = 0; i < storeList.length; ++i) {
            var model = storeList[i];
            var newEle = storeEle.clone();
            //渲染商品Element
            this._renderStoreInfo(newEle, model);
            storeLoopEle.append(newEle);
        }
    } else {//若无商品，提示无商品
        storeEle.remove();
    }
}
//渲染商品信息
Comps.StoreList.prototype._renderStoreInfo = function (newEle, model) {
    var storeIdEle = newEle.find(".j_storeId");
    //图片
    var picEle = newEle.find(".j_pic");
    //图片
    var picHoverEle = newEle.find(".j_picHover");
    //商品名称
    var storeNameEle = newEle.find(".j_storeName");
    /*商品详情路径*/
    var storeUrlEle = newEle.find(".j_storeUrl");
    //收藏量
    var favoriteCountEle = newEle.find(".j_favoriteCount");
    //销售量
    var saleCountEle = newEle.find(".j_saleCount");
    storeIdEle.html(model.uuid);
    picEle.prop("src", model.storeLogo);
    picEle.prop("alt", model.storeName);
    picHoverEle.prop("src", model.storeLogo);
    picHoverEle.prop("alt", model.storeName);
    storeNameEle.prop("href", this.config.storeUrl + model.uuid);
    storeNameEle.html(model.storeName);
    storeUrlEle.prop("href", this.config.storeUrl + model.uuid);
    storeNameEle.prop("title", model.storeName);
    favoriteCountEle.html(model.favoriteCount);
    saleCountEle.html(model.saleCount);
}
//初始化分页栏
Comps.StoreList.prototype._initPages = function (totalNum, pageShow) {
    var ts = this;
    var compId = ts.config.compId;
    layui.use(['laypage', 'layer'], function () {
        var laypage = layui.laypage
            , layer = layui.layer;
        var pages = Math.ceil(totalNum / pageShow);
        if (pages < 1) {
            pages = 1;
        }
        var uri = window.location.href;
        var reg = new RegExp("(^|&)nowPage=([^&]*)");
        var r = uri.match(reg);
        if (!$.isEmptyObject(r)) {
            var curr = r[2];
        }
        laypage({
            cont: compId + '_paging'
            , pages: pages //总页数
            , groups: 5 //连续显示分页数
            , curr: curr
            , jump: function (obj, first) {
                if (!first) {
                    var words = $.isEmptyObject(r) ? "" : r[0];
                    ts.changeUrl(uri, words, "&nowPage=" + obj.curr);
                    ts.ajaxLoadData();
                }
            }
        });
    })
}
//无刷新修改URL
Comps.StoreList.prototype.changeUrl = function (uri, words, replaceWords) {
/*    var title = document.title;
    var state = ({
        url: uri, title: title
    });
    if (!$.isEmptyObject(words)) {
        uri = uri.replace(words, replaceWords);
    } else {
        uri = uri + replaceWords;
    }
    window.history.pushState(state, title, uri);*/
	var title = document.title;
    var state=({
        url: uri, title: title
    });
    if(!$.isEmptyObject(words)&&""!=words){
        uri=uri.replace(words,replaceWords);
    }else{
        uri=uri+replaceWords;
    }
    window.history.pushState(state, title, uri);
}
//获取URL上参数
Comps.StoreList.prototype._getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]);
    return null; //返回参数值
}
Comps.StoreList.prototype._bindEvent = function () {
    var ts = this;
    var compId = this.compId;
    $("a[id^='" + compId + "_sort_']").click(function () {
        var nameValue = $(this).prop("name");
        var sortType = "1";
        var type = $(this).find("i").attr("class");
        if (type == "fa fa-long-arrow-up") {
            sortType = "1";
        } else if (type == "fa fa-long-arrow-down") {
            sortType = "2";
        }
        var queryData = {
            "sortBy": nameValue,
            "sortType": sortType,
            "nowPage": "1"
        }
        $(this).find('.fa').toggleClass('fa-long-arrow-down').toggleClass('fa-long-arrow-up');
        ts.ajaxLoadData(queryData);
    })
}
/**
 * StoreProductList
 * Created by 金辉 on 2017/1/11.
 */
Comps.StoreProductList = function(config){
    this.compId = config.compId;
    this.config = config;
}
//异步情况下渲染数据
Comps.StoreProductList.prototype.init=function(){
    var ts = this;
    var needAsyncInit = ts.config.needAsyncInit;
    var totalNum = $("#"+ts.config.compId+"_productTotalNum").html();
    if(needAsyncInit){
        ts.ajaxLoadData();
    }else{
        Utils.ajaxLoadComplate(ts);
        ts._initPages(totalNum,ts.config.pageShow);
    }
    this._bindEvent();
}
//ajax请求数据
Comps.StoreProductList.prototype.ajaxLoadData =function(queryData){
    var ts = this;
    var defaultData = {
        "compId":ts.config.compId,
        "keyword":ts._getUrlParam("keyword"),
        "storeUuid":ts._getUrlParam("storeUuid"),
        "categoryUuid":ts._getUrlParam("categoryUuid"),
        "startPrice":ts._getUrlParam("startPrice"),
        "endPrice":ts._getUrlParam("endPrice"),
        "sortType":ts._getUrlParam("sortType"),
        "sortBy":ts._getUrlParam("sortBy"),
        "nowPage":ts._getUrlParam("nowPage"),
        "pageShow":ts.config.pageShow

    }
    $.extend(defaultData,queryData);
    $.ajax({
        "url":ContextPath+"/storeProductListComp/ajaxLoadData.json",
        data: defaultData,
        "success":function(data){
            ts._renderProdcutList(data);
            //跳转锚点
            $("html,body").animate({scrollTop:$("#"+ts.config.compId+"_productLoop").offset().top}, 0);
            ts._initPages(data.totalNum,data.pageShow);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
//渲染商品列表
Comps.StoreProductList.prototype._renderProdcutList = function(data){
    var compId = this.config.compId;
    var productsEle = $("#"+compId+"_products");
    var noResultEle = $("#"+compId + "_noResult");
    var productLoopEle = $("#"+compId+"_productLoop");
    var productTotalNumEle = $("#"+compId+"_productTotalNum");
    var productEle = productsEle.find(".j_product").first();
    productsEle.find(".j_product").remove();
    var productList = data.productList;
    //循环渲染商品信息
    if(productList.length>0){
        productTotalNumEle.html(data.totalNum);
        noResultEle.remove();
        for(var i=0;i<productList.length;++i){
            var model = productList[i];
            var newEle = productEle.clone();
            //渲染商品Element
            this._renderProductInfo(newEle,model);
            productLoopEle.append(newEle);
        }
    }else{//若无商品，提示无商品
        productEle.remove();
    }
}
//渲染商品信息
Comps.StoreProductList.prototype._renderProductInfo=function(newEle,model){
    var productIdEle = newEle.find(".j_productId");
    //价格
    var picEle = newEle.find(".j_pic");
    //图片
    var picHoverEle = newEle.find(".j_picHover");
    //评分
    var scoreEle = newEle.find(".j_score");
    //商品名称
    var productNameEle = newEle.find(".j_productName");
    /*商品详情路径*/
    var productUrlEle = newEle.find(".j_productUrl");
    //商品推荐语
    var recommendEle = newEle.find(".j_recommend");
    //商品价格
    var priceEle = newEle.find(".j_price");
    //所属商户名称
    var storeName = newEle.find(".j_storeName");
    //所属商户logo
    var storeLogo = newEle.find(".j_storeLogo");
    productIdEle.html(model.productId);
    if($.isEmptyObject(model.pic)){
        picEle.prop("src","");
        picHoverEle.prop("src","");
    }else{
        picEle.prop("src",model.pic);
        picHoverEle.prop("src",model.pic);
    }
    picEle.prop("alt",model.productName);
    picHoverEle.prop("alt",model.productName);
    scoreEle.prop("style","width:"+model.score*20+"%");
    productNameEle.prop("href",ContextPath+this.config.productInfoUrl+"productUuid="+model.productId);
    productNameEle.html(model.productName);
    productUrlEle.prop("href",ContextPath+this.config.productInfoUrl+"productUuid="+model.productId);
    recommendEle.html(model.recommend);
    priceEle.html(model.price);
    storeName.prop("title",model.sotreName);
    storeLogo.prop("src",model.storeLogo);
}
//初始化分页栏
Comps.StoreProductList.prototype._initPages = function(totalNum,pageShow){
    var ts = this;
    var compId = ts.config.compId;
    layui.use(['laypage', 'layer'], function() {
        var laypage = layui.laypage
            , layer = layui.layer;
        var pages = Math.ceil(totalNum/pageShow);
        if(pages<1){
            pages = 1;
        }
        var uri = window.location.href;
        var reg = new RegExp("(^|&)nowPage=([^&]*)");
        var r = uri.match(reg);
        if(!$.isEmptyObject(r)){
            var curr = r[2];
        }
        laypage({
            cont: compId+'_paging'
            , pages: pages //总页数
            , groups: 5 //连续显示分页数
            ,curr: curr
            ,jump: function(obj, first){
                if(!first) {
                    var words = $.isEmptyObject(r)?"":r[0];
                    ts.changeUrl(uri,words,"&nowPage="+obj.curr);
                    ts.ajaxLoadData();
                }
            }
        });
    })
}
//无刷新修改URL
Comps.StoreProductList.prototype.changeUrl=function(uri,words,replaceWords){
    var title = document.title;
    var state=({
        url: uri, title: title
    });
    if(!$.isEmptyObject(words)){
        uri=uri.replace(words,replaceWords);
    }else{
        uri=uri+replaceWords;
    }
    window.history.pushState(state, title, uri);
}
//获取URL上参数
Comps.StoreProductList.prototype._getUrlParam=function(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]); return null; //返回参数值
}
Comps.StoreProductList.prototype._bindEvent=function(){
    var ts = this;
    var compId = ts.config.compId;
    $("a[id^='"+compId+"_sort_']").click(function(){
        var nameValue = $(this).prop("name");
        var sortType = "1";
        var type = $(this).find("i").attr("class");
        if(type=="fa fa-long-arrow-up"){
            sortType="1";
        }else if(type=="fa fa-long-arrow-down"){
            sortType="2";
        }
        var queryData = {
            "sortBy":nameValue,
            "sortType":sortType,
            "nowPage":"1"
        }
        $(this).find('.fa').toggleClass('fa-long-arrow-down').toggleClass('fa-long-arrow-up');
        ts.ajaxLoadData(queryData);
    })
}

/**
 * StoreSearchComp
 * Created by 金辉 on 2017/1/11.
 */
/**
 * Created by 金辉 on 2017/1/11.
 */
Comps.StoreSearchComp = function (config) {
    this.compId = config.compId;
    this.config = config;
}
Comps.StoreSearchComp.prototype.init = function () {
    var compId = this.compId;
    var keyword = this._getUrlParam("storeKeyword");
    $("#" + compId + "_keyword").val(keyword);
    Utils.ajaxLoadComplate(this);
    this._bindEvent();
}
Comps.StoreSearchComp.prototype.search = function () {
    var compId = this.config.compId;
    var searchUrl = ContextPath+this.config.searchUrl;
    var keyword = $("#" + compId + "_keyword").val();
    var startPrice = $("#" + compId + "_startPrice").val();
    var endPrice = $("#" + compId + "_endPrice").val();
    //获取商铺UUID
    var storeId = this._getUrlParam("storeUuid");
    if($.isEmptyObject(storeId)){
        storeId=this._getStoreId();
    }
    if ($.isEmptyObject(keyword)) {
        keyword = "";
    }
    var url = searchUrl + "&storeUuid=" + storeId + "&storeKeyword=" + keyword;
    if (!$.isEmptyObject(startPrice)) {
        url += "&startPrice=" + startPrice;
    }
    if (!$.isEmptyObject(endPrice)) {
        url += "&endPrice=" + endPrice;
    }
    window.location.href = url;
}
Comps.StoreSearchComp.prototype._getStoreId = function () {
    var productId = this._getUrlParam("productUuid");
    var storeId="";
    $.ajax({
        "url":ContextPath+"/storeSearchComp/getProductStoreId",
        "data":{"productId": productId},
        "async":false,
        "success":function(data){
            storeId =  data;
        }
    })
    return storeId;
}
Comps.StoreSearchComp.prototype._bindEvent = function () {
    var ts = this;
    var compId = this.config.compId;
    $("#" + compId + "_searchProduct").click(function () {
        ts.search();
    })
}
//获取URL上参数
Comps.StoreSearchComp.prototype._getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]);
    return null; //返回参数值
}



/**
 * 美心用商品全文检索框
 * Created by 金辉 on 2017/1/11.
 */
Comps.SearchProductComp = function (config) {
    this.compId = config.compId;
    this.config = config;
}
Comps.SearchProductComp.prototype.init = function () {
    var needAsyncInit = this.config.needAsyncInit;
    var keyword = this._getUrlParam("keyword");
    if(needAsyncInit){
        this._loadData();
        Utils.ajaxLoadComplate(this);
    }
    if(!$.isEmptyObject(keyword)&&keyword!=""){
        $("#"+this.compId+"_keyword").val(keyword);
    }
    this._bindEvent();
}
Comps.SearchProductComp.prototype._loadData = function () {
    var isHaveHot = this.config.isHaveHot;
    var hotwords = this.config.hotWords;
    var hotwordList = "";
    if (!$.isEmptyObject(hotwords)) {
        hotwordList = hotwords.split(",");
    }
    var productSearchUrl = ContextPath + this.config.productSearchUrl;
    var compId = this.config.compId;

    var loopEle = $("#" + compId + "_hotwords");
    var hotwordEle = loopEle.find(".j_hotword");
    if (isHaveHot == "1") {
        for (var i = 0; i < hotwordList.length; ++i) {
            var hotword = hotwordList[i];
            var newEle = hotwordEle.clone();
            newEle.prop("href", productSearchUrl + "&keyword=" + hotword);
            newEle.html(hotword);
            loopEle.append(newEle);
        }
    }
    hotwordEle.remove();
}
Comps.SearchProductComp.prototype.search = function () {
    var compId = this.config.compId;
    var searchUrl;
    searchUrl = ContextPath + this.config.productSearchUrl;
    var keyword = $("#" + compId + "_keyword").val();
    if ($.isEmptyObject(keyword)) {
        keyword = "";
    }
    window.location.href = searchUrl + "?keyword=" + keyword;
}
Comps.SearchProductComp.prototype._bindEvent = function () {
    var ts = this;
    var compId = this.config.compId;
    var searchUrl;
    searchUrl = ContextPath + this.config.productSearchUrl;
    $("#" + compId + "_search").click(function () {
        ts.search();
    })
    //输入框绑定enter事件
    $("#" + compId + "_keyword").bind('keypress',function(event){
	    if(event.keyCode == "13"){
	    	ts.search();
	    }
	})
    $("#" + compId + "_searchType li").click(function () {
        $("#" + compId + "_typeValue").val($(this).data("searchtype"));
        $("#" + compId + "_searchType li").removeClass("current");
        $(this).addClass("current");
    })
    $("#" + ts.compId).delegate('.j_hotword','click',function(){
        window.location.href = searchUrl + "?keyword=" + $(this).html();
    });
}
Comps.SearchProductComp.prototype._getUrlParam=function(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return decodeURIComponent(r[2]); return null; //返回参数值
}
/**
 * 美心前台分类组件
 * Created by 金辉 on 2017/1/11.
 */
Comps.JommoCategoryComp = function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.JommoCategoryComp.prototype.init = function () {
    var needAsyncInit = this.config.needAsyncInit;
    if (needAsyncInit) {
        this.ajaxLoadData();
    }
    this._bindEvent();
}
Comps.JommoCategoryComp.prototype.ajaxLoadData = function () {
    var ts = this;
    var compId = this.config.compId
    $.ajax({
        "url": ContextPath+"/categoryComp/ajaxLoadData.json",
        data: {"compId": compId},
        "success": function (data) {
            ts.render(data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.JommoCategoryComp.prototype.render = function (data) {
    var compId = this.config.compId;
    var showType = this.config.showType;
    var firstLevelNum = this.config.firstLevelNum;
    var dropDiv = $("#" + compId + "_dropDiv");
    var loopEle = $("#" + compId + "_loop");
    var liEle = loopEle.find(".j_firstCatModel");
    if (showType == 0) {
        dropDiv.attr("style", "display:block");
    }
    var length = data.length;
    if (length > firstLevelNum) {
        length = firstLevelNum;
    }
    for (var i = 0; i < length; ++i) {
        var catModel = data[i];
        var newEle = liEle.clone();
        //渲染一级分类，根据配置判断是否带二级分类
        newEle = this._renderFirstCat(newEle, catModel);
        //渲染右侧分类
        newEle = this._renderRitghtCat(newEle, catModel);
        loopEle.append(newEle);
    }
    liEle.remove();
}

/**
 * 渲染一级分类，根据配置判断是否带二级分类
 * @param m
 */
Comps.JommoCategoryComp.prototype._renderFirstCat = function (newEle, m) {
    var showLevel = this.config.showLevel;
    var secondLevelNum = this.config.secondLevelNum;
    var firstCatEle = newEle.find(".j_firstCat");
    var secLoopEle = newEle.find(".j_secCatLoop");
    var secAEle = secLoopEle.find(".j_secCat");
    firstCatEle.html(m.categoryName);
    firstCatEle.prop("href", m.url);

    if (showLevel == 1) {
        var secList = m.subCategory;
        var length = secList.length;
        if (length > secondLevelNum) {
            length = secondLevelNum;
        }
        for (var i = 0; i < length; ++i) {
            var secModel = secList[i];
            var newSecA = secAEle.clone();
            newSecA.prop("href", secModel.url);
            newSecA.html(secModel.categoryName);
            secLoopEle.append(newSecA);
        }
    }
    secAEle.remove();
    return newEle;
}

/**
 * 渲染右侧分类
 * @param m
 */
Comps.JommoCategoryComp.prototype._renderRitghtCat = function (newEle, m) {
    var rightLoop = newEle.find(".j_rightLoop");
    var rightDl = rightLoop.find(".j_rightDl");
    var secList = m.subCategory;
    for (var i = 0; i < secList.length; ++i) {
        var secModel = secList[i];
        var newSecCat = rightDl.clone();
        if (i == 0) {
            newSecCat.addClass('active');
        };

        //分类标签
        var secCat = newSecCat.find(".j_rightSecCat");
        secCat.prop("href", secModel.url);

        //导航条
        var j_catImg = newSecCat.find(".j_catImg");
        j_catImg.attr("src", secModel.img);

        //二级分类名
        var SecName = newSecCat.find(".j_rightSecName");
        SecName.html(secModel.categoryName);

        //二级广告图
        var two_catImg = newSecCat.find(".two_catImg");
        if (secModel.imgAd != '' && secModel.imgAd != null && secModel.imgAd != undefined) {
            two_catImg.attr("src", secModel.imgAd);
        }else{
            two_catImg.remove();
        }

      //二级广告图链接
      if (secModel.imgAdUrl != '' && secModel.imgAdUrl != null && secModel.imgAdUrl != undefined) {
            var towRightSecCat = newSecCat.find(".tow_rightSecCat");
            towRightSecCat.attr("href", secModel.imgAdUrl);
        };        

        rightLoop.append(newSecCat);
    }
    rightDl.remove();
    return newEle;
}
Comps.JommoCategoryComp.prototype._bindEvent = function () {
    var ts = this;
    //全部商品分类列表
    $(".all_classify").click(function(){
       window.location.href = ContextPath + "/items/all.htm";
    });
    //一级菜单做点击事件
    $(".j_firstCat").click(function(){
       var url = $(this).attr("href");
       window.location.href = ContextPath + url;
    });
    //二级导航显示隐藏
    $('.m-categorys').hover(function(){
        $('.m-meun').addClass('show');
    },function(){
        $('.m-meun').removeClass('show');
    });
    $("#" + ts.compId).delegate('.m-meun .m-itme','mouseenter',function(){
        $(this).addClass('active');
    });
    $("#" + ts.compId).delegate('.m-meun .m-itme','mouseleave',function(){
        $(this).removeClass('active');
    });
    $("#"+ts.compId).delegate(".j_firstCat","click",function(){
      var categoryURL = $(this).attr("href");
      window.location.href = categoryURL;
  	})
     $(document).on('mouseenter', '.m-meun .m-three li', function() {
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
}
/**************************顶部购物车组件 start************************************/
Comps.TopAndCartComp= function (compConfig) {
    
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.TopAndCartComp.prototype.init=function(){
    var needAsyncInit = this.config.needAsyncInit;
    
    if (needAsyncInit) {
        this._ajaxLoadData();
    }
    this._bindEvent();
}
Comps.TopAndCartComp.prototype._ajaxLoadData=function(){
    var ts = this;
    var compId = ts.compId;
    ts._render();
    //循环html清空
    $("#"+compId+"_product").html("");
    $("#"+compId+"_totalNum").html("0");
    $("#"+compId+"_totalPrice").html("0.00");
    $.ajax({
        "url": ContextPath+"/topAndCartComp/ajaxLoadData?random="+Math.random(),
        "dataType":"json",
        "success": function (data) {
            ts._renderIsLogin(data.isLogin,data.nickName);
            ts._renderCart(data.data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.TopAndCartComp.prototype._renderIsLogin=function(isLogin,nickName){
    var compId = this.compId;
    var welcomeEle = $("#"+compId+"_welcome");
    
    if(!isLogin){
        var loginUrl = this.config.loginUrl;
        var registreUrl = this.config.registreUrl;
        welcomeEle.html("HI，欢迎来到美心商城，请<a id='"+compId+"_a_login' href='##'>登录</a><b>|</b><a id='"+compId+"_a_register' href='##'>免费注册</a>");
        $("#"+compId+"_a_login").prop("href",loginUrl);
        $("#"+compId+"_a_register").prop("href",registreUrl);
        $("#"+compId+"_a_logout").remove();
    }else{
        welcomeEle.html("HI，<a href='"+ this.config.userCenterUrl +"'>"+nickName+"</a>！欢迎来到美心商城");
    }
}
Comps.TopAndCartComp.prototype._render=function(){
    var compId = this.compId;
    var config = this.config;
    var userCenterUrl = config.userCenterUrl;
    var myOrderUrl = config.myOrderUrl;
    var myCollectionUrl = config.myCollectionUrl;
    var personalDataUrl = config.personalDataUrl;
    var logoutUrl = config.logoutUrl;
    var settlementUrl = config.settlementUrl;
    var helpCenterUrl = config.helpCenterUrl;
    var messageUrl = config.messageUrl;
    var cartUrl = config.cartUrl;
    var globalHome = config.globalHome;
    $("#"+compId+"_a_myCart").prop("href",cartUrl);
    $("#"+compId+"_a_usercenter").prop("href",userCenterUrl);
    $("#"+compId+"_a_myorder").prop("href",myOrderUrl);
    $("#"+compId+"_a_mycollection").prop("href",myCollectionUrl);
    $("#"+compId+"_a_personaldata").prop("href",personalDataUrl);
    $("#"+compId+"_a_logout").prop("href",logoutUrl);
    $(".j_settlement").prop("href",settlementUrl);
    $("#"+compId+"_a_helpCenter").prop("href",helpCenterUrl);
    $("#"+compId+"_a_message").prop("href",messageUrl);
    $("#"+compId+"_a_home").prop("href",globalHome);
}
Comps.TopAndCartComp.prototype._renderCart=function(data){
    var ts = this;
    var compId = this.config.compId;
    var cartEle = $("#"+compId+"_productDiv");
    if ($.isEmptyObject(data)) {
        cartEle.html("<div class='y_cartnothing'>购物车还没有商品、赶紧去选购吧</div>");
        return;
    }
    var cartModuleEle = $("#"+compId+"_productDivModule");
    cartEle.html(cartModuleEle.html());
    var productEle = cartEle.find(".j_product");
    var limodel = $("#"+compId+"_productDivModule").find(".j_productModel");
    productEle.html("");
    $(".j_totalNum").html(data.totalNum);
    $(".j_totalPrice").html(data.totalPrice);
    var productList = data.productList;
    if(productList.length>0) {
        for (var i = 0; i < productList.length; ++i) {
            var product = productList[i];
            var newli = limodel.clone();
            var productId = newli.find(".j_productId");
            var attrId = newli.find(".j_attrId");
            var productPrice = newli.find(".j_productPrice");
            var productNum = newli.find(".j_productNum");
            var productUrl = newli.find(".j_productUrl");
            var productImg = newli.find(".j_productImg");
            var productName = newli.find(".j_productName");
            productId.html(product.productId);
            attrId.html(product.attrId);
            productPrice.html(product.productPrice);
            productNum.html(product.productNum);
            productUrl.prop("href", ContextPath + ts.config.productUrl + "/" + product.productId);
            productName.html(product.productName);
            productName.prop("href", ContextPath + ts.config.productUrl + "/" + product.productId);
            if(product.productImg == '' || product.productImg == undefined){
    			productImg.removeAttr("src");
	    	}else{
	    		productImg.prop("src", product.productImg);
	    	}
            //productImg.prop("src", product.productImg);
            productEle.append(newli);
        }
    }else{
        cartEle.html("<div class='y_cartnothing'>购物车还没有商品、赶紧去选购吧</div>");
    }
}
Comps.TopAndCartComp.prototype._deletePro = function (proEle) {
    var ts = this;
    var compId = this.config.compId;
    var productId = proEle.find(".j_productId").html().trim();
    var attrId = proEle.find(".j_attrId").html().trim();
    $.ajax({
        "url": ContextPath+"/topAndCartComp/deletePro.json",
        "data": {"productId": productId, "attrId": attrId},
        success: function (data) {
            ts._renderCart(data);
          /*  //如果删除成功
            var totalPriceEle = $("#" + compId + "_totalPrice");
            var priceEle = $("#" + compId + "_price");
            var numEle = $("#" + compId + "_num");
            if (!$.isEmptyObject(data)) {
                totalPriceEle.html(data.totalPrice);
                priceEle.html(data.totalPrice);
                numEle.html(data.totalNum);
            } else {
                totalPriceEle.html(0.00);
                priceEle.html(0.00);
                numEle.html(0)
            }
            proEle.remove();*/
        }
    })
}
Comps.TopAndCartComp.prototype._bindEvent=function(){
    var ts = this;
    var compId = this.config.compId;
    var url = ContextPath+"/getUnReadCount";	
		$.getJSON(url,{ranNum:Math.random()},function(data){
			if(data.flag=="true"){
				$("#"+compId+"_a_message").append('<span class="y_label">'+data.count+'</span>');
			}
		})
    $("#" + compId).delegate(".j_deletePro", "click", function () {
        var proEle = $(this).parents(".j_productModel");
        ts._deletePro(proEle);
    });
    //购物车
    $(".m-topbar .m-user").hover(function(){
        $(this).addClass('active');
    },function(){
        $(this).removeClass('active');
    });
    CompMessage.subscribe("refreshTopCart",function(option,callback){
        $.ajax({
            "url": ContextPath+"/topAndCartComp/ajaxLoadData.json",
            "success": function (data) {
                ts._renderIsLogin(data.isLogin,data.nickName);
                ts._renderCart(data.data);
                Utils.ajaxLoadComplate(ts);
                if(callback){
                    callback();
                }
            }
        })
    });
}
/**************************顶部购物车组件 end************************************/
/**************************批发商城顶部购物车组件 start************************************/
Comps.TradeCartComp= function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.TradeCartComp.prototype.init=function(){
    var needAsyncInit = this.config.needAsyncInit;
    if (needAsyncInit) {
        this._ajaxLoadData();
    }
    this._bindEvent();
}
Comps.TradeCartComp.prototype._ajaxLoadData=function(){
    var ts = this;
    var compId = ts.compId;
    ts._render();
    //循环html清空
    $("#"+compId+"_product").html("");
    $("#"+compId+"_totalNum").html("0");
    $("#"+compId+"_totalPrice").html("0.00");
    $.ajax({
        "url": ContextPath+"/tradeCartComp/ajaxLoadData.json",
        "success": function (data) {
            ts._renderIsLogin(data.isLogin,data.nickName);
            ts._renderCart(data.data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.TradeCartComp.prototype._renderIsLogin=function(isLogin,nickName){
    var compId = this.compId;
    var welcomeEle = $("#"+compId+"_welcome");
    
    if(!isLogin){
        var loginUrl = this.config.loginUrl;
        var registreUrl = this.config.registreUrl;
        welcomeEle.html("HI，欢迎来到美心商城，请<a id='"+compId+"_a_login' href='##'>登录</a>");
        $("#"+compId+"_a_login").prop("href",loginUrl);
        $("#"+compId+"_a_logout").remove();
    }else{
        welcomeEle.html("HI，<a href='"+ this.config.userCenterUrl +"'>"+nickName+"</a>！欢迎来到美心商城");
    }
}
Comps.TradeCartComp.prototype._render=function(){
    var compId = this.compId;
    var config = this.config;
    var userCenterUrl = config.userCenterUrl;
    var myOrderUrl = config.myOrderUrl;
    var myCollectionUrl = config.myCollectionUrl;
    var personalDataUrl = config.personalDataUrl;
    var logoutUrl = config.logoutUrl;
    var settlementUrl = config.settlementUrl;
    var helpCenterUrl = config.helpCenterUrl;
    var messageUrl = config.messageUrl;
    var cartUrl = config.cartUrl;
    var globalHome = config.globalHome;
    $("#"+compId+"_a_myCart").prop("href",cartUrl);
    $("#"+compId+"_a_usercenter").prop("href",userCenterUrl);
    $("#"+compId+"_a_myorder").prop("href",myOrderUrl);
    $("#"+compId+"_a_mycollection").prop("href",myCollectionUrl);
    $("#"+compId+"_a_personaldata").prop("href",personalDataUrl);
    $("#"+compId+"_a_logout").prop("href",logoutUrl);
    $("#"+compId+"_a_helpCenter").prop("href",helpCenterUrl);
    $("#"+compId+"_a_message").prop("href",messageUrl);
    $("#"+compId+"_a_home").prop("href",globalHome);
    $(".j_settlement").prop("href",settlementUrl);
}
Comps.TradeCartComp.prototype._renderCart=function(data){
    var ts = this;
    var compId = this.config.compId;
    var cartEle = $("#"+compId+"_productDiv");
    if ($.isEmptyObject(data)) {
        cartEle.html("<div class='y_cartnothing'>购物车还没有商品、赶紧去选购吧</div>");
        return;
    }
    var cartModuleEle = $("#"+compId+"_productDivModule");
    cartEle.html(cartModuleEle.html());
    var productEle = cartEle.find(".j_product");
    var limodel = $("#"+compId+"_productDivModule").find(".j_productModel");
    productEle.html("");
    $(".j_totalNum").html(data.totalNum);
    $(".j_totalPrice").html(data.totalPrice);
    var productList = data.productList;
    if(productList.length>0) {
        for (var i = 0; i < productList.length; ++i) {
            var product = productList[i];
            var newli = limodel.clone();
            var productId = newli.find(".j_productId");
            var attrId = newli.find(".j_attrId");
            var productPrice = newli.find(".j_productPrice");
            var productNum = newli.find(".j_productNum");
            var productUrl = newli.find(".j_productUrl");
            var productImg = newli.find(".j_productImg");
            var productName = newli.find(".j_productName");
            productId.html(product.productId);
            attrId.html(product.attrId);
            productPrice.html(product.productPrice);
            productNum.html(product.productNum);
            productUrl.prop("href", ContextPath + ts.config.productUrl + "/" + product.productId);
            productName.html(product.productName);
            productName.prop("href", ContextPath + ts.config.productUrl + "/" + product.productId);
            productImg.prop("src", product.productImg);
            productEle.append(newli);
        }
    }else{
        cartEle.html("<div class='y_cartnothing'>购物车还没有商品、赶紧去选购吧</div>");
    }
}
Comps.TradeCartComp.prototype._deletePro = function (proEle) {
    var ts = this;
    var compId = this.config.compId;
    var productId = proEle.find(".j_productId").html().trim();
    var attrId = proEle.find(".j_attrId").html().trim();
    $.ajax({
        "url": ContextPath+"/tradeCartComp/deletePro.json",
        "data": {"productId": productId, "attrId": attrId},
        success: function (data) {
            ts._renderCart(data);
            /*  //如果删除成功
             var totalPriceEle = $("#" + compId + "_totalPrice");
             var priceEle = $("#" + compId + "_price");
             var numEle = $("#" + compId + "_num");
             if (!$.isEmptyObject(data)) {
             totalPriceEle.html(data.totalPrice);
             priceEle.html(data.totalPrice);
             numEle.html(data.totalNum);
             } else {
             totalPriceEle.html(0.00);
             priceEle.html(0.00);
             numEle.html(0)
             }
             proEle.remove();*/
        }
    })
}
Comps.TradeCartComp.prototype._bindEvent=function(){
    var ts = this;
    var compId = this.config.compId;
    $("#" + compId).delegate(".j_deletePro", "click", function () {
        var proEle = $(this).parents(".j_productModel");
        ts._deletePro(proEle);
    });
    CompMessage.subscribe("refreshTradeCart",function(option,callback){
        $.ajax({
            "url": ContextPath+"/tradeCartComp/ajaxLoadData.json",
            "success": function (data) {
                ts._renderIsLogin(data.isLogin,data.nickName);
                ts._renderCart(data.data);
                Utils.ajaxLoadComplate(ts);
                if(callback){
                    callback();
                }
            }
        })
    });
}
/**************************批发商城顶部购物车组件 end************************************/
/**************************面包屑组件 start************************************/
Comps.BreadRumbNavigationComp= function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.BreadRumbNavigationComp.prototype.init=function(){
    var that = this;
    Utils.ajaxLoadComplate(that);
}
Comps.BreadRumbNavigationComp.prototype._ajaxLoadData=function(){
    var ts = this;
    $.ajax({
        "url": ContextPath+"/breadRumbNavigation/ajaxLoadData.json",
        "data":{"categoryUuid":ts.config.categoryUuid},
        "success": function (data) {
            ts._render(data);
        }
    })
}
Comps.BreadRumbNavigationComp.prototype._render=function(data){
    var ts = this;
    var config = ts.config;
    var compId = this.compId;
    $("#"+compId+"_index").attr("href",config.indexUrl);
    var loopEle = $("#"+compId+"_loop");
    var separateEle = loopEle.find(".j_separate");
    var categoryEle = loopEle.find(".j_categoryName");
    for(var i=0;i<data.length;++i){
        var separateClone = separateEle.clone();
        var categoryClone = categoryEle.clone();
        var category = data[i];
        categoryClone.attr("href",category.categoryUrl);
        categoryClone.html(category.categoryName);;
        loopEle.append(separateClone);
        if(data.length-1==i){
            loopEle.append(category.categoryName);
        }else{
            loopEle.append(categoryClone);
        }
    }
    separateEle.remove();
    categoryEle.remove();
}
Comps.BreadRumbNavigationComp.prototype._bindEvent=function(){
}
/**************************面包屑组件 end************************************/
/**************************商用商品搜索框组件 start************************************/
Comps.CommercialSearchComp= function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.CommercialSearchComp.prototype.init=function(){
    this._bindEvent();
    Utils.ajaxLoadComplate(this);
}
Comps.CommercialSearchComp.prototype._bindEvent=function(){
    var compId = this.compId;
    var config = this.config;
    var keyword = $("#"+compId+"_keyword").val();
    $("#"+compId+"_search").click(function(){
        window.location.href = ContextPath + config.searchUrl+"?keyword="+keyword;
    })
}
/**************************商用商品搜索框组件 end************************************/
/**************************商用商品分类组件 end************************************/
Comps.CommercialChannelComp= function (compConfig) {
    this.compId = compConfig.compId;
    this.config = compConfig;
}
Comps.CommercialChannelComp.prototype.init=function(){
    this._bindEvent();
    Utils.ajaxLoadComplate(this);
}
Comps.CommercialChannelComp.prototype._bindEvent=function(){
    var compId = this.compId;
    var config = this.config;
    var keyword = $("#"+compId+"_keyword").val();
    $("#"+compId+"_search").click(function(){
        window.location.href = ContextPath + config.searchUrl+"?keyword="+keyword;
    })
    $(".j_commercial").hover(function(){
        $("#"+compId+"_box").fadeIn();
    },function(){
        $("#"+compId+"_box").fadeOut();
    })
    $("#" + compId).delegate(".commercial-tab li a","mouseover",function() {
        $(this).addClass('tab-a-border').parent().siblings().children().removeClass('tab-a-border');
        $(".commercial-tab-box").hide().eq($(".commercial-tab li a").index(this)).show();
    });
    $("#" + compId).delegate(".commercial-t-i-a","mouseover",function() {
        $(this).addClass('tab-a-border').parent().siblings().children().removeClass('tab-a-border');
    });
}
/**************************商用商品分类框组件 end************************************/